package hbase;

/**
 * Hello world!
 *
 */
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.Delete;

import java.io.IOException;
public class HBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(String[] args)throws IOException{
        init();
        //建表
        
        createTable("student",new String[]{"S_No","S_Information"});
        createTable("course",new String[]{"C_No","C_Information"});
        createTable("sc",new String[]{"SC_Information"});
        createTable("sc1",new String[]{"course","stu1","stu2","stu3"});
        createTable("sc2",new String[]{"course1","course2","course3"});
        //插入数据
        insertData("student","2015001","S_Information","S_Name","Li Lei");
        insertData("student","2015001","S_Information","S_Sex","male");
        insertData("student","2015001","S_Information","S_Age","23");
        insertData("student","2015002","S_Information","S_Name","Han Meimei");
        insertData("student","2015002","S_Information","S_Sex","female");
        insertData("student","2015002","S_Information","S_Age","22");
        insertData("student","2015003","S_Information","S_Name","Zhang San");
        insertData("student","2015003","S_Information","S_Sex","male");
        insertData("student","2015003","S_Information","S_Age","24");

        insertData("course","123001","C_Information","C_Name","Math");
        insertData("course","123001","C_Information","C_Credit","2.0");        
        insertData("course","123002","C_Information","C_Name","Computer Science");
        insertData("course","123002","C_Information","C_Credit","5.0");   
        insertData("course","123003","C_Information","C_Name","English");
        insertData("course","123003","C_Information","C_Credit","3.0");

        insertData("sc","1","SC_Information","SC_Sno","2015001");
        insertData("sc","1","SC_Information","SC_Cno","123001");
        insertData("sc","1","SC_Information","SC_Score","86");
        insertData("sc","2","SC_Information","SC_Sno","2015001");
        insertData("sc","2","SC_Information","SC_Cno","123003");
        insertData("sc","2","SC_Information","SC_Score","69");
        insertData("sc","3","SC_Information","SC_Sno","2015002");
        insertData("sc","3","SC_Information","SC_Cno","123002");
        insertData("sc","3","SC_Information","SC_Score","77");
        insertData("sc","4","SC_Information","SC_Sno","2015002");
        insertData("sc","4","SC_Information","SC_Cno","123003");
        insertData("sc","4","SC_Information","SC_Score","99");
        insertData("sc","5","SC_Information","SC_Sno","2015003");
        insertData("sc","5","SC_Information","SC_Cno","123001");
        insertData("sc","5","SC_Information","SC_Score","98");
        insertData("sc","6","SC_Information","SC_Sno","2015003");
        insertData("sc","6","SC_Information","SC_Cno","123002");
        insertData("sc","6","SC_Information","SC_Score","95");

        insertData("sc1","123001","course","c_name","Math");
        insertData("sc1","123001","stu1","id","2015001");
        insertData("sc1","123001","stu1","name","Li Lei");
        insertData("sc1","123001","stu1","score","86");
        insertData("sc1","123001","stu3","id","2015003");
        insertData("sc1","123001","stu3","name","Zhang San");
        insertData("sc1","123001","stu3","score","98");
        insertData("sc1","123002","course","c_name","Computer Science");
        insertData("sc1","123002","stu2","id","2015002");
        insertData("sc1","123002","stu2","name","Han Meimei");
        insertData("sc1","123002","stu2","score","77");
        insertData("sc1","123002","stu3","id","2015003");
        insertData("sc1","123002","stu3","name","Zhang San");
        insertData("sc1","123002","stu3","score","95");
        insertData("sc1","123003","course","c_name","English");
        insertData("sc1","123003","stu1","id","2015001");
        insertData("sc1","123003","stu1","name","Li Lei");
        insertData("sc1","123003","stu1","score","69");
        insertData("sc1","123003","stu2","id","2015002");
        insertData("sc1","123003","stu2","name","Han Meimei");
        insertData("sc1","123003","stu2","score","99");
        
        insertData("sc2","2015001","course1","id","123001");
        insertData("sc2","2015001","course1","score","86");
        insertData("sc2","2015001","course3","id","123003");
        insertData("sc2","2015001","course3","score","69");
        insertData("sc2","2015002","course2","id","123002");
        insertData("sc2","2015002","course2","score","77");
        insertData("sc2","2015002","course3","id","123003");
        insertData("sc2","2015002","course3","score","99");
        insertData("sc2","2015003","course1","id","123001");
        insertData("sc2","2015003","course1","score","98");
        insertData("sc2","2015003","course2","id","123002");
        insertData("sc2","2015003","course2","score","95");
        System.out.println("student");
        scanTable("student");
        System.out.println("course");
        scanTable("course");
        System.out.println("sc");
        scanTable("sc");
        System.out.println("sc1");
        scanTable("sc1");
        System.out.println("sc2");
        scanTable("sc2");

        //查询选修Computer 
        //getData("sc1","123002","stu1","name");
        //getData("sc1","123002","stu1","score");
       //getData("sc1","123002","stu2","name");
        //getData("sc1","123002","stu2","score");
        //getData("sc1","123002","stu3","name");
        //getData("sc1","123002","stu3","score");
        queryTableByRowKey("sc1","123002");
        //插入新列
        /*
        insertData("students","001","Courses","English","95");
        insertData("students","002","Courses","English","85");
        insertData("students","003","Courses","English","98");
        */
        //插入新列族
        addnewColumn("student", "Contact");
        insertData("student","20150001","Contact","Email","lilei@qq.com");
        insertData("student","2015002","Contact","Email","hmm@qq.com");
        insertData("student","2015003","Contact","Email","zs@qq.com");
        //扫描一遍以证明上述插入操作成功
        scanTable("student");
        deleteByRowKey("sc2","2015003");
        scanTable("sc2");
        //删除表
        dropTable("student");
        dropTable("course");
        dropTable("sc");
        dropTable("sc1");
        System.out.println("all done");
        close();
    }

    //初始化
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","lxy191870098-master");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    //结束
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    //创建函数
    public static void createTable(String myTableName,String[] colFamily) throws IOException {
        TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for(String str:colFamily){
                ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
            System.out.println("talbe creation successful!");
        }
    }

    //插入函数
    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
        table.put(put);
        table.close();
    }

    //查询函数
    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close();
    }
    public static void queryTableByRowKey(String tableNameString,String rowNameString) throws IOException {
        //取得数据表对象
        Table table=connection.getTable(TableName.valueOf(tableNameString));
        //新建一个查询对象作为查询条件
        Get get = new Get(rowNameString.getBytes());
        //按行查询数据
        Result result = table.get(get);
        byte[] row = result.getRow();
        //System.out.println("row key is:" +new String(row));
        List<Cell> listCells = result.listCells();
        for (Cell cell : listCells) {
            System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            /*byte[] familyArray = cell.getFamilyArray();
            byte[] qualifierArray = cell.getQualifierArray();
            byte[] valueArray = cell.getValueArray();
            System.out.println( new String(familyArray) + "  "+
                    new String(qualifierArray) +"  "+ new String(valueArray));*/
        }
    }
    //扫描函数
    public static void scanTable(String tableName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();

        ResultScanner results = table.getScanner(scan);
        for (Result result : results) {
            for (Cell cell : result.rawCells()) {
                System.out.println(
                        "key:" + new String(CellUtil.cloneRow(cell)) + "\t" +
                                "family:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
                                "name:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
                                "value:" + new String(CellUtil.cloneValue(cell)) + "\t" +
                                "time:" + cell.getTimestamp());
            }
        }
        table.close();
    }

    //插入新列族函数
    public static void addnewColumn(String tableName,String column) throws IOException{
        HColumnDescriptor newFamliy = new HColumnDescriptor(column);
        admin.addColumn(TableName.valueOf(tableName), newFamliy);
        System.out.println("adding column successful!");
    }
    public static void deleteByRowKey(String tableNameString, String rowKey) throws IOException {

        //获取待操作的数据表对象
        Table table =connection.getTable(TableName.valueOf(tableNameString));
        //创建删除条件对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
    }

    //删除表函数
    public static void dropTable(String tableName) throws IOException{

        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println("dropping table successful!");

    }
}
